package com.grgbanking.it.common.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import com.grgbanking.it.common.Constant;
import com.grgbanking.it.common.Message;
import com.grgbanking.it.project.entity.OperationWork;


/**
 * 手机短信工具
 * @author pgwei
 *
 */
public class PhoneMessageUtil {
	//外网地址
//	private String service = "http://116.254.254.34/general/procedures/itfix/msg_ymrt.php";
	//内网地址
	private String service = "http://10.252.252.34/general/procedures/itfix/msg_ymrt.php";
	private String sign = "【GRG】";
	private String encode = "UTF-8";
	private static PhoneMessageUtil common = new PhoneMessageUtil();
	
	public static PhoneMessageUtil getInstance() {
		return common;
	}
	
	/**
	 * &Mobile=*&Content=
	 * @throws Exception 
	 */
	private void sent(String mobile,String content) throws Exception{
		String contentWithSign = URLEncoder.encode(new StringBuffer(content).append(sign).toString(), encode);  
		StringBuffer message = new StringBuffer(service);
		message.append("?Mobile=").append(mobile).append("&Content=")
				.append(contentWithSign);
		
		URL url = new URL(message.toString());

		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.connect();

		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));
		String lines; 

        while ((lines = reader.readLine()) != null) { 
//            System.out.println(lines); 
        } 

        reader.close(); 

        // 断开连接 
        connection.disconnect(); 
	}
	
	/**
	 * 工作分配后发送短信
	 * @param mobile
	 * @param username
	 * @throws Exception 
	 */
	public void sentWhenWorkCreated(String mobile) throws Exception{
		String content = Message.WORK_CREATED;
		sent(mobile, content);
	}
	
	/**
	 * 工作完成后发送短信
	 * @param mobile
	 * @param username
	 * @throws Exception 
	 */
	public void sentWhenWorkFinish(String mobile) throws Exception{
		String content = "";
		sent(mobile, content);
	}
	
	/**
	 * 通知工程师进行工作
	 * 
	 * @param mobile
	 * @param work
	 * @throws Exception 
	 */
	public void sentToEngineer(String mobile,OperationWork work) throws Exception{
		
		sent(mobile, Message.getDistributeMessageToEngineer(work));
	}
	
	public static void main(String[] args){
		try {
			PhoneMessageUtil.getInstance().sent("18565084465", "中文1111");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
